package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.UserDao;
import com.itheima.health.dao.UserRoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.User;
import com.itheima.health.pojo.UserRole;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;


@Slf4j
@DubboService
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public User findByName(String username) {
        User newUser = new User();
        newUser.setUsername(username);
        User user = userDao.selectOne(newUser);
        log.info("[user表-findByName-返回值] - user: {}",user);
        return user;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<User> page = userDao.findPage(queryPageBean.getQueryString());
        log.info("[user表-findPage-返回值] - page: {}",page);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Transactional
    @Override
    public boolean userAdd(User user, Integer[] roleIds) {
        log.info("[user表-userAdd-接收值] - user: {} roleIds: {}",user,roleIds);
        boolean flag = false;
        userDao.insertSelective(user);
        Integer userId = user.getId();
        for (Integer roleId : roleIds) {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRoleDao.insertSelective(userRole);
            flag = true;
        }
        return flag;
    }

    @Override
    public User findById(Integer id) {
        log.info("[user表-findById-接收值] - id: {}",id);
        User user = userDao.selectByPrimaryKey(id);
        log.info("[user表-findById-返回值] - user: {}",user);
        return user;
    }

    @Transactional
    @Override
    public boolean update(User user,Integer[] roleIds) {
        log.info("[user表-update-接收值] - user: {} - roleIds: {}",user,roleIds);
        boolean flag = false;
        int updateCount = userDao.updateByPrimaryKeySelective(user);
        Integer userId = user.getId();
        Example example = new Example(UserRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        userRoleDao.deleteByExample(example);

        if (roleIds.length>0){
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            for (Integer roleId : roleIds) {
                userRole.setRoleId(roleId);
                userRoleDao.insertSelective(userRole);
            }
            flag = true;
        }
        if (updateCount > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean deleteById(Integer id) {
        log.info("[user表-deleteById-接收值] - id: {}",id);
        boolean flag = false;
        Example example = new Example(UserRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",id);
        int deleteUserRoleCount = userRoleDao.deleteByExample(example);
        if (deleteUserRoleCount>0){
            int deleteUserCount = userDao.deleteByPrimaryKey(id);
            if (deleteUserCount>0){
                flag = true;
            }
        }
        return flag;
    }
}
